import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:solar/modal/api.dart';
import 'package:solar/modal/banner.dart';

import 'brower.dart';
import 'detail.dart';

class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _currentIndex = 0;
  List bannerArr = [];

  List jqList = [];
  String jijieName = '春季';

  // banner 图
  List<BannerItem> initBanner(arr) {
    List<BannerItem> bannerList = [];
    List.generate(arr.length, (index) {
      bannerList.add(
          BannerItem.defaultBannerItem(arr[index]["url"], arr[index]["sub"]));
    });
    return bannerList;
  }

  // 春节
  List<Widget> jieqiList(arr) {
    List<Widget> jqList = [
      SizedBox(height: 10),
      Row(
        children: [
          SizedBox(width: 15),
          Text(jijieName,
              style: TextStyle(
                fontSize: 20,
                fontWeight: FontWeight.w400,
              )),
        ],
      ),
      SizedBox(height: 40),
    ];
    List.generate(arr.length, (index) {
      jqList.add(Container(
          width: MediaQuery.of(context).size.width / 3,
          child: InkWell(
            onTap: () => {
              Navigator.push(
                context,
                MaterialPageRoute(
                  builder: (context) => DetailPage(pageId: arr[index]['url']),
                ),
              )
            },
            child: Column(
              children: [
                Stack(
                  children: [
                    Image.network(arr[index]['bg1']),
                    Image.network(arr[index]['bg2'])
                  ],
                ),
                Container(
                  child: Text(
                    arr[index]['time'],
                    style: TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.w400,
                        color: Colors.black),
                  ),
                ),
                SizedBox(
                  height: 15,
                )
              ],
            ),
          )));
    });
    return jqList;
  }

  @override
  void initState() {
    EasyLoading.show();
    getJieqiList(0).then((res) {
      EasyLoading.dismiss();
      print("dddd$res");
      setState(() {
        jqList = res['list'];
        jijieName = res['name'];
        bannerArr = res['bannerList'];
      });
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Container(
        child: Column(
          children: [
            new BannerWidget(
              180.0,
              initBanner(bannerArr),
              bannerPress: (pos, item) {
                print(bannerArr[pos]['goUrl']);
                Navigator.of(context).push(new MaterialPageRoute(builder: (_) {
                  return new Browser(
                    url: bannerArr[pos]['goUrl'],
                    title: "",
                  );
                }));
              },
            ),
            SizedBox(
              height: 10,
            ),
            Container(
                width: MediaQuery.of(context).size.width,
                child: Wrap(children: [...jieqiList(jqList)]))
          ],
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        items: [
          BottomNavigationBarItem(
              icon: Container(
                width: 50,
                height: 50,
                child: Image.asset('assets/images/0.png'),
              ),
              title: new Text(
                '',
              )),
          BottomNavigationBarItem(
              icon: Container(
                width: 50,
                height: 50,
                child: Image.asset('assets/images/1.png'),
              ),
              title: new Text(
                '',
              )),
          BottomNavigationBarItem(
              icon: Container(
                width: 50,
                height: 50,
                child: Image.asset('assets/images/2.png'),
              ),
              title: new Text(
                '',
              )),
          BottomNavigationBarItem(
              icon: Container(
                width: 50,
                height: 50,
                child: Image.asset('assets/images/3.png'),
              ),
              title: new Text(
                '',
              ))
        ],
        currentIndex: _currentIndex,
        onTap: (int i) {
          setState(() {
            _currentIndex = i;
            EasyLoading.show();
            getJieqiList(_currentIndex).then((res) {
              EasyLoading.dismiss();
              setState(() {
                jqList = res['list'];
                jijieName = res['name'];
                bannerArr = res['bannerList'];
              });
            });
          });
        },
      ),
    );
  }
}
